
def solve(inpt):
    #Receives a string input of symbols
    #Figures out the # of unique symbols and assigns them in a way that generates the smallest possible number (by assigning smallest to most significant)
    #Uses the dictionary lookup to find the base 10 value of this number
    #Returns that smallest possible number
    baseSet = set()
    baseDict = {}
    smallestNumUsed = 1;
    for letter in inpt:
##        #debug
##        print "letter: ", letter, baseSet
        baseSet.add(letter)
        if len(baseSet) == 2 and letter not in baseDict:
            baseDict[letter] = 0 #special case: the 2nd different digit has to be 0. A number can't start with 0
        elif letter not in baseDict:
            baseDict[letter] = smallestNumUsed
            smallestNumUsed += 1

    #debug:
##    print "base dict: ", baseDict
##    print "base set: ", baseSet


    #After all the numbers are mapped, the smallest meaning possible is one where the base is the number of unique symbols observed
    base = smallestNumUsed #number of symbols ~~ +1 for zero
##    print "base: ", base

    #smallestNumberPossible = convert from dictionary
    digitPower = 0
    smallestNumberPossible = 0
    for digit in inpt[::-1]: #Shorthand to reverse inpt "::" is called the stride operator
        smallestNumberPossible += (base ** digitPower) * baseDict[digit]
        digitPower += 1
    return smallestNumberPossible 

outp = open("out.txt",'w')
inf = open(str(raw_input('Enter infile name: ')))
T = int(inf.readline())
for count in range(T):
    inpt = str(inf.readline()).strip('\n')
    outp.write("Case #" + str(count + 1) + ": " + str(solve(inpt)))
    outp.write("\n")

outp.close()
inf.close()
